package com.xidian.GMS.service.impl;

import java.util.Date;
import java.util.List;

import javax.swing.table.TableModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xidian.GMS.dto.Page;
import com.xidian.GMS.entity.*;
import com.xidian.GMS.mapper.AnnouncementMapper;
import com.xidian.GMS.service.AnnouncementManage;;
@Service
public class AnnouncementManageImpl implements AnnouncementManage {
	@Autowired
	private AnnouncementMapper announcementMapper;
	
	
	@Override
	public Announcement Insert( String title, Date uploadtime,String content) {
		
	   Announcement announcement = new Announcement();
	  System.out.println(title);
    	announcement.setAnnouncementTitle(title);;
    	announcement.setAnnouncementUploadtime(uploadtime);
    	announcement.setAnnouncemengContent(content);
   	  int rows = announcementMapper.insert(announcement);
		if( announcement == null ||rows == 0){
			throw new RuntimeException("错误");
		}
          return announcement;
    }
	

	@Override
	public boolean delete(int announcementid) {
        int x =  announcementMapper.deleteByPrimaryKey(announcementid);
  	  if (x == 1) {
  	  	  return true;
	}
          return false;
    }	

	@Override
	public boolean update(int announcementid, String title, String content,Date edittime) {
		   Announcement announcement2 = announcementMapper.selectByPrimaryKey(announcementid);
			
			announcement2.setAnnouncementTitle(title);
	    	announcement2.setAnnouncementUploadtime(edittime);
	    	announcement2.setAnnouncemengContent(content);
	   	 
		
           int temp = announcementMapper.updateByPrimaryKeySelective(announcement2);
		
       	  if (temp == 1) {
      	  	  return true;
    	}
              return false;
        }

	@Override
	public Page getAnnouncements(int cur) {
		Page page = new Page(cur);
		
		int count = announcementMapper.count();
		
		List<Announcement> accounts = announcementMapper.findAll(page.getOffset(), page.getSize());
		
		page.setCount(count);
		page.setList(accounts);
		
		return page;
	}
}
